|
|
|
|
|
|
|
Displaying Text with a Label Control |
|
|
|
|
|
|
|
|
The purpose of the Label control is to display text. It is most often used to identify items on a form to the users. The simplest way to use the Label control is to place it next to an input field and set its Caption property. Refer to Figure 4.2 and notice that the Label controls are used to inform the users what type of information is to be entered in the text boxes. |
|
|
|
|
|
|
|
|
Although the Label control does not allow the users to enter text directly, it contains events and properties that allow the programmer to manipulate the text directly from code. To test this feature, create a new Standard EXE project and place a Label control on the form. Place the following code in the label's Click event: |
|
|
|
|
|
|
|
|
Private Sub Label1_Click()
Label1 = "The time is: " & Time$
End Sub |
|
|
|
|
|
|
|
|
Run the program. Each time you click in the label using the mouse, the label displays the current time. Also, in the preceding code, notice that you do not specifically mention the Caption property because it is the default property of the Label control. As you may recall, to specify a property of an object, you use dot notation, as in the following line of code: |
|
|
|
|
|
|
|
|
Label1.Caption = "The time is: " & Time$ |
|
|
|
|
|
|
|
|
However, you are not limited to using the label control to display small amounts of text. In fact, you can use the Label control to display multiple lines or even paragraphs of information. If you want to display a large amount of information in a label control, you need to pay particular attention to the AutoSize and WordWrap properties. |
|
|
|
|
|
|
|
|
If you know in advance what text is going to be displayed in a label's Caption property, you can set the size of the label to accommodate the text. However, if different text will be displayed in the label at different times (for example, in a database application), you may want the label to adjust to the length of its current contents. The AutoSize property of the Label control determines whether the size of the control automatically adjusts to fit the text being displayed. When AutoSize is False (the default), the label's size remains unchanged regardless of the length of its caption. If a caption is too long for the label, some of the caption is not visible because it's wider than the label, and it does not wrap to another line. |
|
|
|
|
|
|
|
|
Setting AutoSize to True causes a label to adjust its size automatically to fit its caption. If the caption is longer than the label's original size allows, the method of resizing depends on the value of the WordWrap property. If the WordWrap property is False (the default), the label expands horizontally to allow the caption to fit, even if the label grows so large that it runs past the right edge of the screen. If the WordWrap property is set to True, the label expands vertically to allow enough lines of text to accommodate the caption, even if the label runs off the bottom edge of the screen. (The words wrap to new lineshence, the property name WordWrap.) In either case, the Caption property contains the entire caption, even if some of the text isn't visible on the form. The effects of the different settings of the AutoSize and WordWrap properties are shown in Figure 4.3. |
|
|
|
|
|